﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using verk2013_project_group12.Models;


namespace verk2013_project_group12.Models
{
    public class GameInstanceRepository
    {
        AppDataContext db = new AppDataContext();


        public IEnumerable<GameInstance> GetAllInstances()
        {
            return db.GameInstances;
        }

        public GameInstance GetInstanceById(int id)
        {
            var theInstance = (from i in db.GameInstances
                              where i.ID == id
                              select i).SingleOrDefault();
            return theInstance;
        }

        public IEnumerable<GameInstance> GetInstancesByGameId(int id)
        {
            var theInstances = from i in db.GameInstances
                               where i.GameID == id
                               select i;

            return theInstances;
        }


        public void Save()
        {
           
            db.SaveChanges();
        }


        public void Add(GameInstance instance)
        {
           
            instance.Draw = false;
            instance.Running = false;

            db.GameInstances.Add(instance);
        }

        public void Remove(GameInstance instance)
        {
            if (instance != null)
            {
                db.GameInstances.Attach(instance);
                db.GameInstances.Remove(instance);
            }
        }

    }
}